package com.wireless.service;

import java.util.HashMap;
import java.util.Map;

public final class ChargeDeviceRepo {
	
	private ChargeDeviceRepo(){}
	
	/**
	 * get instance of chargeDeviceList
	 * @return
	 */
	public static ChargeDeviceRepo getInstance(){
		if(chargeDeviceRepo == null)
			chargeDeviceRepo = new ChargeDeviceRepo();
		return chargeDeviceRepo;
	}
	
	/**
	 * add device into repo
	 * @param deviceID
	 * @param device
	 */
	public void addDevice(String deviceID,ChargeDevice device){
		synchronized (deviceMapRepo) {
			deviceMapRepo.put(deviceID, device);
		}
	}
	
	/**
	 * when the device is out of network from in network,should delete it from repo
	 * @param deviceID
	 */
	public void deleteDevice(String deviceID) {
		synchronized (deviceMapRepo) {
			deviceMapRepo.remove(deviceID);
		}
	}
	
	/**
	 * judge if deviceMapRepo exist the ChargeDevice
	 * @param deviceID
	 * @return
	 */
	public boolean existDevice(String deviceID) {
		if(deviceID !=null){
			if(deviceMapRepo.get(deviceID) != null)
				return true;
		}
		return false;
	}
	
	/**
	 * get chargeDevice by deviceID
	 * if the deviceMapRepo doesn't exist the device return null 
	 * @param deviceID
	 * @return
	 */
	public ChargeDevice findChargeDeviceByID(String deviceID){
		if(deviceID == null || deviceMapRepo == null)
			return null;
		return deviceMapRepo.get(deviceID);
	}
	
	private static ChargeDeviceRepo chargeDeviceRepo =null;
	private static Map<String, ChargeDevice> deviceMapRepo= new HashMap<String,ChargeDevice>();//new map when load
}
